package com.flow.demo.module.gd.service.list;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flow.demo.module.gd.controller.app.list.dto.GoodMySelfDTO;
import com.flow.demo.module.gd.controller.app.list.vo.ListEditVO;
import com.flow.demo.module.gd.controller.app.list.vo.ListPageReqVO;
import com.flow.demo.module.gd.controller.app.list.vo.ListSaveReqVO;
import com.flow.demo.module.gd.dal.dataobject.good.GoodDO;
import com.flow.demo.module.gd.dal.mysql.good.GoodMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.flow.demo.module.gd.dal.dataobject.list.ListDO;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.util.object.BeanUtils;

import com.flow.demo.module.gd.dal.mysql.list.ListMapper;

import javax.annotation.Resource;

import java.util.Objects;

import static com.flow.demo.framework.common.exception.enums.GlobalErrorCodeConstants.LIST_NOT_EXISTS;
import static com.flow.demo.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户获奖名单 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ListServiceImpl implements ListService {

    @Resource
    private ListMapper listMapper;

    @Resource
    private GoodMapper goodMapper;

    @Override
    public Integer createList(ListSaveReqVO createReqVO) {
        // 插入
        ListDO list = BeanUtils.toBean(createReqVO, ListDO.class);
        listMapper.insert(list);
        // 返回
        return list.getId();
    }

    @Override
    public void updateList(ListEditVO updateReqVO) {
        // 校验存在
        validateListExists(updateReqVO.getId());
        // 更新
        ListDO updateObj = BeanUtils.toBean(updateReqVO, ListDO.class);
        listMapper.updateById(updateObj);
    }

    @Override
    public void deleteList(Integer id) {
        // 校验存在
        validateListExists(id);
        // 删除
        listMapper.deleteById(id);
    }

    private void validateListExists(Integer id) {
        if (listMapper.selectById(id) == null) {
            throw exception(LIST_NOT_EXISTS);
        }
    }

    @Override
    public ListDO getList(Integer id) {
        return listMapper.selectById(id);
    }

    @Override
    public PageResult<ListDO> getListPage(ListPageReqVO pageReqVO) {
        return listMapper.selectPage(pageReqVO);
    }

    @Override
    public GoodMySelfDTO getGoodMySelf(Long loginUserId) {
        ListDO listDO = listMapper.selectOne(new QueryWrapper<ListDO>().lambda().eq(ListDO::getGdUserId, loginUserId));
        if(Objects.isNull(listDO))
            return null;

        Integer gdGoodId = listDO.getGdGoodId();//商品id

        GoodDO goodDO = goodMapper.selectOne(new QueryWrapper<GoodDO>().lambda().eq(GoodDO::getPrizeId, gdGoodId));

        GoodMySelfDTO bean = BeanUtils.toBean(goodDO, GoodMySelfDTO.class);
        bean.setReceive(listDO.getReceive());
        bean.setListId(listDO.getId());
        return bean;
    }

}
